package com.ty.springbootjpamybatis;

import com.ty.springbootjpamybatis.entity.User;
import com.ty.springbootjpamybatis.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
 * UserMapperTester
 *
 * @aurhor Administrator  whs
 * @since 2024/9/3
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTester {

    @Resource
    private UserMapper userMapper;

    @Test
    public void testInsert() throws Exception { // 添加
        userMapper.insert(new User("ktjiaoyu","123456",1));
    }

    @Test
    public void testGet() throws Exception { // 查询
        try {
            // MyBatis 查询操作
            User user = userMapper.get(88L);
            System.out.println("userName: " + user.getUsrName());
        } catch (NumberFormatException e) {
            System.out.println("数据类型转换错误：" + e.getMessage());
        }
    }

    @Test
    public void testUpdate() throws Exception { // 修改
        userMapper.update(new User(88L,"小猫老弟","77777",1l,1));
    }

    @Test
    public void testDelete() throws Exception { // 删除
        userMapper.delete(84L);
    }

    @Test
    public void testFindAll() throws Exception { // 查询
        List<User> users = userMapper.findAll();
        for (User user : users) {
            System.out.println(user);
        }
    }

}
